/************************************************************************
 * File: mainwindow.cpp                                                 *
 *                                                                      *
 * Copyright 2006, 2008 Colin Kern (colin.kern@gmail.com)               *
 *                                                                      *
 * This file is part of Profit.  Some previous versions were released   *
 * under the names Megacorp Mania and Acquire.                          *
 *                                                                      *
 * Profit is free software: you can redistribute it and/or modify       *
 * it under the terms of the GNU General Public License as published by *
 * the Free Software Foundation, either version 3 of the License, or    *
 * any later version.                                                   *
 *                                                                      *
 * Profit is distributed in the hope that it will be useful,            *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
 * GNU General Public License for more details.                         *
 *                                                                      *
 * You should have received a copy of the GNU General Public License    *
 * along with Profit.  If not, see <http://www.gnu.org/licenses/>.      *
 ************************************************************************/
#include "mainwindow.h"
#include "gamemanager.h"
#include "globals.h"
#include <QtGui>
#include <QTime>

MainWindow::MainWindow()
{
	m_game = new GameManager();
	
	setCentralWidget(m_game);
	
	createActions();
	createMenus();
	createDockWidgets();
	
	QTime midnight(0, 0, 0);
	qsrand(midnight.secsTo(QTime::currentTime()));
}

void MainWindow::closeEvent(QCloseEvent *event)
{
	if (okToContinue()) {
		event->accept();
	} else {
		event->ignore();
	}
}

void MainWindow::newGame()
{
	if (okToContinue()) {
		m_game->newGame();
	}
}

void MainWindow::about()
{
	QMessageBox::about(this, tr("About Profit"),
		tr("<h2>Profit v") + Profit::Version + tr("</h2>"
			"<p>Copyright &copy; 2006, 2008 Colin Kern (colin.kern@gmail.com)"
			"<p>Profit is based on the board game Acquire. Visit "
			"<a href=\"http://groups.google.com/group/tantalus-games/web/profit\">"
			"http://groups.google.com/group/tantalus-games/web/profit</a>"
			" for more information."
			"<p>This program comes with ABSOLUTELY NO WARRANTY; "
		    "This is free software, and you are welcome to redistribute it "
		    "under certain conditions; see the file LICENSE for more details."));
}

void MainWindow::createActions()
{
	m_newAction = new QAction(tr("&New"), this);
	m_newAction->setShortcut(tr("Ctrl+N"));
	connect(m_newAction, SIGNAL(triggered()), this, SLOT(newGame()));
	
	m_quitAction = new QAction(tr("E&xit"), this);
	m_quitAction->setShortcut(tr("Ctrl+Q"));
	connect(m_quitAction, SIGNAL(triggered()), this, SLOT(close()));
	
	m_aboutAction = new QAction(tr("&About"), this);
	connect(m_aboutAction, SIGNAL(triggered()), this, SLOT(about()));
	
	m_aboutQtAction = new QAction(tr("About &Qt"), this);
	connect(m_aboutQtAction, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
}

void MainWindow::createMenus()
{
	m_gameMenu = menuBar()->addMenu(tr("&Game"));
	m_gameMenu->addAction(m_newAction);
	m_gameMenu->addSeparator();
	m_gameMenu->addAction(m_quitAction);
	
	m_viewMenu = menuBar()->addMenu(tr("&View"));
	
	m_helpMenu = menuBar()->addMenu(tr("&Help"));
	m_helpMenu->addAction(m_aboutAction);
	m_helpMenu->addAction(m_aboutQtAction);
}

void MainWindow::createDockWidgets()
{
	QDockWidget *dock = new QDockWidget("Message Log");
	dock->setWidget(m_game->getMessageLog());
	addDockWidget(Qt::RightDockWidgetArea, dock);
	m_viewMenu->addAction(dock->toggleViewAction());
}

bool MainWindow::okToContinue()
{
	if (m_game->gameRunning()) {
		int result = QMessageBox::warning(this, tr("Profit"),
			tr("There is currently a game in progress.\n"
			   "Close the current game?"),
			   QMessageBox::Yes,
			   QMessageBox::Cancel | QMessageBox::Default | QMessageBox::Escape);
		if (result == QMessageBox::Yes) {
			return true;
		} else if (result == QMessageBox::Cancel) {
			return false;
		}
	}
	return true;
}